package com.budwk.app.sqfwV2.generator.caringStaff;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BaseExcelGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelDataInto;
import com.budwk.app.sqfwV2.dto.BriefDTO;
import com.budwk.app.sqfwV2.dto.CaringStaffDTO;
import com.budwk.app.sqfwV2.dto.image.AuthenticRightConfigBo;
import com.budwk.app.sqfwV2.enums.CaringStaffEnum;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.security.utils.SecurityUtil;

import java.util.*;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.addPictureToByteList;

public class CaringStaffExcelGenerator extends BaseExcelGenerator {

    private String templateName;



    public CaringStaffExcelGenerator(){}

    public CaringStaffExcelGenerator(String templateName){
        this.templateName=templateName;
    }





    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        return true;
    }


    @Override
    protected void customerPopularHeaderData(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        CommonParam createUserCommonParam =  excelGenerateParam.getDownloadUserParam();
        excelParam.put("county",createUserCommonParam.getCounty()+"党工委");
        excelParam.put("downDate",DateUtil.format(new Date(), DatePattern.CHINESE_DATE_PATTERN));
    }

    @Override
    protected void customerPopularHeaderDataForAdmin(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeErgodic(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected boolean addRowDataCondition(ExcelRowDataParam rowDataParam, ExcelGenerateParam excelGenerateParam) {
        return true;
    }

    @Override
    protected void customerPopularSheetData(List<Map<String, Object>> listMap, ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {
        CommonParam createUserCommonParam =  rowDataParam.getCreateUserCommonParam();
        CaringStaffDTO caringStaffDTO = (CaringStaffDTO) rowDataParam.getAbstractCustomerPdfCreateField();
        if("直属".equals( createUserCommonParam.getCommunity())){
            rowDataMap.put("county",  createUserCommonParam.getCounty());
        }else {
            if(!createUserCommonParam.getCommunity().contains("社区")){
                createUserCommonParam.setCommunity(createUserCommonParam.getCommunity()+"社区");
            }
            rowDataMap.put("county",  createUserCommonParam.getCounty()+createUserCommonParam.getCommunity());
        }

        if(IdcardUtil.isValidCard(caringStaffDTO.getIDNumber())){
            rowDataMap.put("age", IdcardUtil.getAgeByIdCard(caringStaffDTO.getIDNumber()));
            rowDataMap.put("gender", IdcardUtil.getGenderByIdCard(caringStaffDTO.getIDNumber())==1?"男":"女");
            rowDataMap.put("birth",DateUtil.format( IdcardUtil.getBirthDate(caringStaffDTO.getIDNumber()),"yyyy.MM"));
        }else {
            if(StrUtil.isNotEmpty(caringStaffDTO.getDateOfBirth())){
                rowDataMap.put("age", DateUtil.ageOfNow(caringStaffDTO.getDateOfBirth()));
            }else {
                rowDataMap.put("age", "0");
            }

            rowDataMap.put("gender",caringStaffDTO.getGender());
            rowDataMap.put("birth",DateUtil.format(DateUtil.parse(caringStaffDTO.getDateOfBirth()),"yyyy.MM") );
        }
        String partyStanding="";
        if(ObjectUtil.isNotEmpty(caringStaffDTO.getPartyMembershipTime())){
               partyStanding=DateUtil.betweenYear(caringStaffDTO.getPartyMembershipTime(),new Date(),true)+"年";
        }
        rowDataMap.put("partyStanding",partyStanding);
        List<BriefDTO> list=  caringStaffDTO.getBriefList();
        if(CollectionUtil.isNotEmpty(list)){
            for (BriefDTO briefDTO : list) {
                if("familyDifficulties".equals(briefDTO.getKey())){
                    rowDataMap.put("familyDifficulties", briefDTO.getWhetherOrNot().equals("否")?"否":briefDTO.getProblemContent());
                }
                if("needHelp".equals(briefDTO.getKey())){
                    rowDataMap.put("needHelp", briefDTO.getWhetherOrNot().equals("否")?"否":briefDTO.getProblemContent());
                }
            }
        }
        if(StrUtil.isNotEmpty(caringStaffDTO.getVisitPictures())){
            rowDataMap.put("visitPictures", addPictureToByteList(Arrays.asList(caringStaffDTO.getVisitPictures().split(",")).get(0), 1));
        } else {
            rowDataMap.put("visitPictures", null);
        }
        rowDataMap.put("partyMembershipTime", DateUtil.format(caringStaffDTO.getPartyMembershipTime(),"yyyy.MM.dd"));
        rowDataMap.put("remark", "");
        List<String> strings=Arrays.asList("光荣在党五十年,困难党员,重大贡献党员".split(","));
        List<String> strings1=new ArrayList<>();
        for (String s : strings) {
            if( caringStaffDTO.getCareCategory().contains(s)){
                strings1.add(s);
            }
        }
        rowDataMap.put("careCategory", String.join(",",strings1));
    }

    @Override
    protected void customerPopularRowDataMapForAdmin(Map<String, Object> rowDataMap, ExcelRowDataParam rowDataParam) {

    }

    @Override
    protected ResultOfExcelDataInto judgeExcelHaveDataBeforeCreate(Map<String, Object> excelParam, ExcelGenerateParam excelGenerateParam) {
        return null;
    }

    @Override
    protected String getTemplatePath(ExcelGenerateParam excelGenerateParam) {
        return this.templateName;
    }

    @Override
    protected String getDownloadFileName(ExcelGenerateParam excelGenerateParam) {
        String code = excelGenerateParam.getExcelConfig().getFileIdentity();
        return CaringStaffEnum.getFileName(Integer.parseInt(code))+DateUtil.format(new Date(),"yyyyMMddHHmmss");
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return AuthenticRightConfigBo.class;

    }

    @Override
        protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParamBO) {
//            excelFileCreateParamBO.setImgWidth(180);
//            excelFileCreateParamBO.setRowHeight(122);
        }


}
